import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from common.Untils import QESDriver


class MyPractice:
    def __init__(self):
        self.url = "http://8.155.1.153:8081/exercise"
        self.driver = QESDriver.driver
        self.driver.get(self.url)
        # 设置显式等待的最大超时时间为10秒
        self.wait = WebDriverWait(self.driver, 10)

    def GetMyPractice(self):
        try:
            # 使用显式等待查找元素
            title1 = self.wait.until(
                EC.presence_of_element_located((By.XPATH,
                                                '//*[@id="sk-layout"]/div/div/section/div[2]/main/div/div[2]/div/div/div[2]/div[2]/div/div[1]/div/div[1]/div'))
            ).text
            assert title1 == "我的练习"

            # 其他标题元素的查找也使用类似的显式等待方式
            title2 = self.wait.until(
                EC.presence_of_element_located((By.XPATH,
                                                '//*[@id="sk-layout"]/div/div/section/div[2]/main/div/div[2]/div/div/div[2]/div[2]/div/div[2]/div/div/div/div/div/table/thead/tr/th[1]'))
            ).text
            assert title2 == "练习名称"

            # 省略其他标题的断言代码...

            QESDriver.GetScreenShot()
        except Exception as e:
            print(f"GetMyPractice方法出错: {str(e)}")
            QESDriver.GetScreenShot()
            raise

    def Numxercise(self):
        try:
            # 找到"123"标签并点击
            self.wait.until(
                EC.element_to_be_clickable((By.XPATH, '//*[@id="rc-tabs-0-tab-123"]/span'))
            ).click()
            time.sleep(1)

            # 查找输入框并输入“测试部门”
            input_box = self.wait.until(
                EC.presence_of_element_located((By.XPATH, '//*[@id="name"]'))
            )
            input_box.clear()  # 清空输入框
            input_box.send_keys("测试部门")

            # 查找“查询”并点击
            self.wait.until(
                EC.element_to_be_clickable((By.XPATH,
                                            '//*[@id="sk-layout"]/div/div/section/div[2]/main/div/div[2]/div/div/div[2]/div[1]/form/div/div[2]/div/div/div[2]/div/div/div/div/div/div[2]/button/span'))
            ).click()

            time.sleep(1.5)
            QESDriver.GetScreenShot()

            # 查找“重置”并点击
            self.wait.until(
                EC.element_to_be_clickable((By.XPATH,
                                            '//*[@id="sk-layout"]/div/div/section/div[2]/main/div/div[2]/div/div/div[2]/div[1]/form/div/div[2]/div/div/div[2]/div/div/div/div/div/div[1]/button/span'))
            ).click()

            # 查找“开始练习”并点击
            self.wait.until(
                EC.element_to_be_clickable((By.XPATH,
                                            '//*[@id="sk-layout"]/div/div/section/div[2]/main/div/div[2]/div/div/div[2]/div[2]/div/div[2]/div/div/div/div/div/table/tbody/tr[4]/td[3]/div/div/a'))
            ).click()

            QESDriver.GetScreenShot()

            # 避免使用/html/body这种绝对路径，尽量使用相对路径
            title1 = self.wait.until(
                EC.presence_of_element_located((By.XPATH,
                                                '//div[contains(text(), "顺序练习")]'))  # 使用更灵活的定位方式
            ).text
            assert title1 == "顺序练习"

            title2 = self.wait.until(
                EC.presence_of_element_located((By.XPATH,
                                                '//div[contains(text(), "随机练习")]'))
            ).text
            assert title2 == "随机练习"

            title3 = self.wait.until(
                EC.presence_of_element_located((By.XPATH,
                                                '//div[contains(text(), "错题练习")]'))
            ).text
            assert title3 == "错题练习"

        except Exception as e:
            print(f"Numxercise方法出错: {str(e)}")
            QESDriver.GetScreenShot()  # 出错时截图
            raise
